package com.estronger.shopmanage.adapter.batchpg;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.batch.BatchPgListBean;
import com.estronger.shopmanage.bean.batch.DeliverPGListData;
import com.estronger.shopmanage.bean.batch.InstallPGListData;
import com.estronger.shopmanage.bean.batch.MeasurePGListData;
import com.estronger.shopmanage.utils.CustomLinearLayoutManager;

import java.util.ArrayList;

/**
 * author : DC-DingYG
 * e-mail : dingyg012655@126.com
 * time : 2020/09/16
 * desc : * 待批量派工数据list适配器
 */
public class BatchPgListAdapter extends BaseAdapter implements View.OnClickListener{

    ArrayList<BatchPgListBean> mList;
    Context mContext;
    AdapterListener adapterListener;

    public BatchPgListAdapter(Context context) {
        mContext = context;
    }

    public void setAdapterListener(AdapterListener adapterListener) {
        this.adapterListener = adapterListener;
    }

    public void notifyDataSetChanged(ArrayList<BatchPgListBean> dataList) {
        this.mList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    @Override
    public BatchPgListBean getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.size() <= 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final BatchPgViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_pg_batch, null);
            holder = new BatchPgViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (BatchPgViewHolder) convertView.getTag();
        }
        holder.setData(mList.get(position), position);
        holder.llfMeasureItem.setTag(holder);
        holder.llfMeasureItem.setOnClickListener(this);
//        holder.rl_check.setTag(holder);
//        holder.rl_check.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        BatchPgViewHolder vh = (BatchPgViewHolder) v.getTag();
        switch (v.getId()){
//            case R.id.rl_check:
//
//                break;
            case R.id.llMeasureItem:
                BatchPgListBean listBean = mList.get(vh.positon);
                if(listBean.getFcheck() == 1){//去掉勾选
                    listBean.setFcheck(0);
                    vh.iv_check.setImageResource(R.drawable.confirm_check_bt);
                }else {
                    listBean.setFcheck(1);
                    vh.iv_check.setImageResource(R.drawable.checked);
                }
                if(adapterListener != null) {
                    adapterListener.onCheckChange(CheckChange());
                }
                break;
        }
    }

    public boolean CheckChange(){
        boolean isShowAllCheck = true;//是否全选了
        for (BatchPgListBean listBean : mList){
            if(listBean.getFcheck() == 0){
                isShowAllCheck = false;
                break;
            }
        }
        return isShowAllCheck;
    }

    public class BatchPgViewHolder{

        public int positon;
        public LinearLayout llfMeasureItem;
        public TextView tvfItemName, tvfItemLocation, tvfOrderTimeT, tvfOrderTime, tvftem_phoneno;
        public RelativeLayout rl_check;
        public ImageView iv_check;
        public BatchPgViewHolder(View itemView) {
            llfMeasureItem = (LinearLayout) itemView.findViewById(R.id.llMeasureItem);
            tvfItemName = (TextView) itemView.findViewById(R.id.tvfItemTitle);
            tvfItemLocation = (TextView) itemView.findViewById(R.id.tvfItemLocation);
            tvfOrderTimeT = (TextView) itemView.findViewById(R.id.tvfOrderTimeT);
            tvfOrderTime = (TextView) itemView.findViewById(R.id.tvfOrderTime);
            tvftem_phoneno = (TextView) itemView.findViewById(R.id.tvftem_phoneno);
            rl_check = itemView.findViewById(R.id.rl_check);
            iv_check = itemView.findViewById(R.id.iv_check);
        }

        public void setData(BatchPgListBean listBean, int position) {
            this.positon = position;
            if(listBean.getFcheck() == 1){
                iv_check.setImageResource(R.drawable.checked);
            }else {
                iv_check.setImageResource(R.drawable.confirm_check_bt);
            }
            if(listBean.getDatatype() == 1){//测量派工
                MeasurePGListData data = listBean.getMeasure();
                if (TextUtils.isEmpty(data.cstbuildname)) {
                    tvfItemName.setText(data.getCstname());
                } else {
                    tvfItemName.setText(data.cstbuildname + "-" + data.getCstname());
                }
                tvftem_phoneno.setText(data.getCstmobile());
                tvfItemLocation.setText(data.getAddress());
                tvfOrderTimeT.setText("下单时间:");
                tvfOrderTime.setText(data.getOrdrdate());
            }else if(listBean.getDatatype() == 2){//送货派工
                DeliverPGListData data = listBean.getDeliver();
                if (TextUtils.isEmpty(data.buildname)) {
                    tvfItemName.setText(data.getCstname());
                } else {
                    tvfItemName.setText(data.buildname + "-" + data.getCstname());
                }
                tvftem_phoneno.setText(data.getCstmobile());
                tvfItemLocation.setText(data.getAddress());
                tvfOrderTimeT.setText("开单时间:");
                tvfOrderTime.setText(data.getCdate());
            }else if(listBean.getDatatype() == 3){//安装派工
                InstallPGListData data = listBean.getInstall();
                if (TextUtils.isEmpty(data.cstbuildname)) {
                    tvfItemName.setText(data.getCstname());
                } else {
                    tvfItemName.setText(data.cstbuildname + "-" + data.getCstname());
                }
                tvftem_phoneno.setText(data.getCstmobile());
                tvfItemLocation.setText(data.getAddress());
                tvfOrderTimeT.setText("预计安装时间:");
                tvfOrderTime.setText(data.getPidate());
            }
        }
    }
    public interface AdapterListener {
        void onCheckChange(boolean isCheckedAll);//是否选中了所有的
    }

    /**
     * RecyclerView 移动到当前位置，
     *
     * @param manager       设置RecyclerView对应的manager
     * @param mRecyclerView 当前的RecyclerView
     * @param n             要跳转的位置
     */
    public void MoveToPosition(CustomLinearLayoutManager manager, RecyclerView mRecyclerView, int n) {
        int firstItem = manager.findFirstVisibleItemPosition();
        int lastItem = manager.findLastVisibleItemPosition();
        if (n <= firstItem) {
            mRecyclerView.scrollToPosition(n);
        } else if (n <= lastItem) {
            int top = mRecyclerView.getChildAt(n - firstItem).getTop();
            mRecyclerView.scrollBy(0, top);
        } else {
            mRecyclerView.scrollToPosition(n);
        }
    }
}
